using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace hzWeb.Features.CreateChatRoomList
{
    /// <summary>
    /// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
    /// </summary>
    /// <remarks>
    /// 附加到此类的 GUID 可能会在打包期间使用，不应进行修改。
    /// </remarks>

    [Guid("803f0337-3dd6-41ab-8564-bb6f1463acc5")]
    public class CreateChatRoomListEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;

            using (SPWeb roontWeb = siteCollection.OpenWeb(""))
            {
                if (!ContainsList(roontWeb.Lists, "聊天室") && !ContainsList(roontWeb.Lists, "群组"))
                {
                    var chatRoomsTemplate = roontWeb.ListTemplates["ChatRooms"];
                    var guid = roontWeb.Lists.Add("chatrooms", "群组列表", chatRoomsTemplate);
                    SPList chatRoom = roontWeb.Lists[guid];
                    chatRoom.Title = "群组";
                    chatRoom.Update();
                }
                else if (ContainsList(roontWeb.Lists, "聊天室"))
                {
                    SPList roomList = roontWeb.Lists["聊天室"];
                    roomList.Title = "群组";
                    roomList.Update();
                }

                SPList chatRoomList = roontWeb.Lists["群组"];                
                if (null != chatRoomList)
                {
                    if (!chatRoomList.HasUniqueRoleAssignments)
                    {
                        chatRoomList.OnQuickLaunch = true;
                        chatRoomList.BreakRoleInheritance(false);
                        SPRoleDefinitionCollection roleDefinitions = siteCollection.RootWeb.RoleDefinitions;
                        SPUser newUser = siteCollection.RootWeb.EnsureUser(Config.Instance.supperAdminGroup);
                        SPRoleAssignment roleAssignment = new SPRoleAssignment(newUser);
                        SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;

                        roleDefBindings.Add(Helper.GetRoleDefinition(roleDefinitions, "Full Control"));
                        chatRoomList.RoleAssignments.Add(roleAssignment);
                        chatRoomList.Update();
                    }
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;
            string pubFeatureID = "F6924D36-2FA8-4f0b-B16D-06B7250180FA";
            Guid PublishingInfraFeatureID = new Guid(pubFeatureID);
            siteCollection.Features.Add(PublishingInfraFeatureID, true, SPFeatureDefinitionScope.Site);
        }

        private bool ContainsList(SPListCollection listCollection, string listName)
        {
            foreach (SPList list in listCollection)
            {
                if (list.Title == listName)
                {
                    return true;
                }
            }

            return false;
        }

        // 取消对以下方法的注释，以便处理在安装某个功能后引发的事件。

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }


        // 取消对以下方法的注释，以便处理在卸载某个功能前引发的事件。

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // 取消对以下方法的注释，以便处理在升级某个功能时引发的事件。

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
